package com.dolphin.apidriver.controller;

import com.dolphin.apidriver.service.ApiDriverOrderInfoService;
import com.dolphin.common.dto.ResponseResult;
import com.dolphin.common.dto.TokenResult;
import com.dolphin.common.request.OrderRequest;
import com.dolphin.common.util.JwtUtils;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/order")
public class OrderInfoController {
    @Autowired
    ApiDriverOrderInfoService orderInfoService;

    @PostMapping("/grab")
    @GlobalTransactional
    public ResponseResult grab(@RequestBody OrderRequest order, HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("Authorization");
        //从token中获取司机信息
        TokenResult tokenResult = JwtUtils.parseToken(token);
        String identity = tokenResult.getIdentity();
        String driverPhone = tokenResult.getPhone();

        Long orderId = order.getOrderId();

        String receiveOrderCarLatitude = order.getReceiveOrderCarLatitude();
        String receiveOrderCarLongitude = order.getReceiveOrderCarLongitude();

        return orderInfoService.grab(driverPhone,orderId,receiveOrderCarLongitude,receiveOrderCarLatitude);
    }

    /**
     * 去接乘客
     * @param orderRequest
     * @return
     */
    @PostMapping("/to-pick-up-passenger")
    public ResponseResult toPickUpPassenger(@RequestBody OrderRequest orderRequest) {

        return orderInfoService.toPickUpPassenger(orderRequest);
    }

    /**
     * 到达乘客目的地
     * @param orderRequest
     * @return
     */
    @PostMapping("/arrive-departure")
    public ResponseResult arriveDeparture(@RequestBody OrderRequest orderRequest) {


        return orderInfoService.arriveDeparture(orderRequest);
    }
    /**
     * 司机接到乘客  乘客上车
     * @param orderRequest
     * @return
     */
    @PostMapping("/pick-up-passenger")
    public ResponseResult pickUpPassenger(@RequestBody OrderRequest orderRequest) {

        return orderInfoService.pickUpPassenger(orderRequest);
    }
    /**
     * 乘客到达目的地  乘客下车
     * @param orderRequest
     * @return
     */
    @PostMapping("/passenger-getoff")
    public ResponseResult passengerGetOff(@RequestBody OrderRequest orderRequest) {

        return orderInfoService.passengerGetOff(orderRequest);
    }
    @PostMapping("/cancel")
    ResponseResult cancel(@RequestParam Long orderId){
        return orderInfoService.cancel(orderId);
    }
}
